package com.cloud.mkz.admin.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cloud.mkz.admin.api.dto.AuthedUser;
import com.cloud.mkz.admin.api.dto.UserBean;
import com.cloud.mkz.admin.api.entity.RbacUser;
import com.cloud.mkz.admin.api.entity.RbacUserRole;
import com.cloud.mkz.admin.service.RbacUserRoleService;
import com.cloud.mkz.admin.service.RbacUserService;
import com.cloud.mkz.core.common.constant.CacheConstants;
import com.cloud.mkz.core.common.util.ResponseResult;
import com.cloud.mkz.core.mvc.controller.BaseController;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * TODO
 *
 * @author: KuangJianyu
 * @date: 2021-3-16
 */
@RestController
@RequestMapping("/passport")
@RequiredArgsConstructor
public class PassportController extends BaseController {
    private final RbacUserService rbacUserService;
    private final RbacUserRoleService rbacUserRoleService;

    @PostMapping("/login")
    public ResponseResult<UserBean> login(@RequestParam("randomStr") String randomStr,
                                          @RequestParam("code") String code,
                                          @RequestParam("lgun") String lgun,
                                          @RequestParam("cipherup") String cipherup){
        if(StrUtil.isBlank(randomStr)){
            return ResponseResult.failed("异常访问");
        }
        if(StrUtil.isBlank(code)){
            return ResponseResult.failed("请输入验证码");
        }
        Object pvalue = redisUtil.get(CacheConstants.DEFAULT_CODE_KEY+randomStr);
        if(null==pvalue){
            return ResponseResult.failed(2, "验证码已失效");
        }
        if(!code.equals(pvalue.toString())){
            redisUtil.del(CacheConstants.DEFAULT_CODE_KEY+randomStr);
            return ResponseResult.failed(2, "验证码错误");
        }
        QueryWrapper<RbacUser> wrapper = new QueryWrapper<>();
        wrapper.eq("login_acc", lgun).eq("login_pwd ", cipherup);
        List<RbacUser> rbacUsers = rbacUserService.list(wrapper);
        if(null==rbacUsers||rbacUsers.size()<1){
            return ResponseResult.failed(3,"用户名或密码错误");
        }
        if(rbacUsers.size()>1){
            return ResponseResult.failed(4,"数据错误，记录不唯一");
        }
        RbacUser rbacUser = rbacUsers.get(0);
        List<RbacUserRole> userRoles = rbacUserRoleService.getRolesByUserId(rbacUser.getPkId());
        if(CollectionUtil.isEmpty(userRoles)){
            return ResponseResult.failed(5,"此用户没有任何角色");
        }
        return rbacUserService.login(randomStr, rbacUser, userRoles);
    }

    /**
     * 用户退出登录
     * @param token
     * @return ResponseResult
     */
    @PostMapping("/logout")
    public ResponseResult logout(@RequestParam("x-auth-token") String token){
        Object o = redisUtil.get(CacheConstants.PROJECT_OAUTH_TOKEN + token);
        if(null==o){
            return ResponseResult.ok("用户登录已退出");
        }
        redisUtil.del(CacheConstants.PROJECT_OAUTH_TOKEN + token);
        List<Object> usersOnline = redisUtil.lGetAll(CacheConstants.SYSTEM_ONLINE_DETAILS);
        for (Object userOnline : usersOnline) {
            AuthedUser authedUser = (AuthedUser)userOnline;
            if(token.equals(authedUser.getAuthToken())){
                redisUtil.lRemove(CacheConstants.SYSTEM_ONLINE_DETAILS, 1, userOnline);
            }
        }
        return ResponseResult.ok("登录退出成功");
    }

    /**
     * 判断当前token是否已经登录
     * @param token
     * @return ResponseResult
     */
    @PostMapping("/checkToken")
    public ResponseResult checkToken(@RequestParam("x-auth-token") String token){
        Object o = redisUtil.get(CacheConstants.PROJECT_OAUTH_TOKEN + token);
        if(null!=o){
            return ResponseResult.ok("用户已登录");
        }
        return ResponseResult.failed("用户未登录");
    }
}
